package com.morder.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.morder.R;
import com.morder.application.AppPreference;
import com.morder.util.MyScrollLayout;
import com.morder.util.OnViewChangeListener;

public class NavigationActivity extends Activity implements OnViewChangeListener {

  private MyScrollLayout mScrollLayout;
  private ImageView[] imgs;
  private int count;
  private int currentItem;
  private Button startBtn;
  private LinearLayout pointLLayout;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.navigation);
    AppPreference.markFirstBootFinished(true);
    initView();
  }

  private void initView() {
    mScrollLayout = (MyScrollLayout) findViewById(R.id.ScrollLayout);
    pointLLayout = (LinearLayout) findViewById(R.id.llayout);
    startBtn = (Button) findViewById(R.id.startBtn);
    startBtn.setOnClickListener(onClick);
    count = mScrollLayout.getChildCount();
    imgs = new ImageView[count];
    for (int i = 0; i < count; i++) {
      imgs[i] = (ImageView) pointLLayout.getChildAt(i);
      imgs[i].setEnabled(true);
      imgs[i].setTag(i);
    }
    currentItem = 0;
    imgs[currentItem].setEnabled(false);
    mScrollLayout.SetOnViewChangeListener(this);
  }

  private View.OnClickListener onClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      switch (v.getId()) {
      case R.id.startBtn:
        mScrollLayout.setVisibility(View.GONE);
        pointLLayout.setVisibility(View.GONE);
        Intent intent = new Intent(NavigationActivity.this, MainActivity.class);
        NavigationActivity.this.startActivity(intent);
        NavigationActivity.this.finish();
        break;
      }
    }
  };

  @Override
  public void OnViewChange(int position) {
    setcurrentPoint(position);
  }

  private void setcurrentPoint(int position) {
    if (position < 0 || position > count - 1 || currentItem == position) {
      return;
    }
    imgs[currentItem].setEnabled(true);
    imgs[position].setEnabled(false);
    currentItem = position;
  }

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && (event.getRepeatCount() == 0)) {
      Intent intent = new Intent(NavigationActivity.this, MainActivity.class);
      NavigationActivity.this.startActivity(intent);
      NavigationActivity.this.finish();
      return true;
    }
    return super.onKeyDown(keyCode, event);
  }
}
